<template>
  <section class="content">
    <el-col :span="24">
      <el-button type="primary" class="button-add" @click="useradd = true">
        <i class="el-icon-edit"></i>添加成员
      </el-button>
    </el-col>
    <el-col :span="24">
      <el-table :data="tableData" stripe highlight-current-row class="table">
        <el-table-column type="index" width="60"></el-table-column>
        <el-table-column prop="role" label="角色"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="phoneNum" label="手机号"></el-table-column>
        <el-table-column prop="email" label="邮件"></el-table-column>
        <el-table-column prop="operation" label="操作">
          <template slot-scope="scope">
            <el-button @click="deleteRow(scope.$index, scope.row)" type="text" size="small">移除</el-button>
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)" size="small">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <span class="show-pager">显示总数</span>
        <el-pagination
          @current-change="handleCurrentChange"
          @pre-click="handlePreClick"
          @next-click="handleNextClick"
          :current-page.sync="currentPage"
          :page-size="10"
          layout="total, prev, pager, next"
          :total="totalRecord"
        ></el-pagination>
      </div>
    </el-col>

    <el-dialog title="添加成员" :visible.sync="useradd" :modal="false" >
      <el-form :model="form">
        <el-form-item label="姓名：" :label-width="formLabelWidth" >
          <el-select v-model="userList" filterable placeholder="请输入关键词" @change="userListSelectKey">
            <el-option
              v-for="item in tableData3"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
              <!-- <el-pagination
                    @current-change="handleCurrentChange1"
                    @pre-click="handlePreClick1"
                    @next-click="handleNextClick1"
                    :current-page.sync="currentPage1"
                    :page-size="100"
                    layout="total, prev, pager, next"
                    :total= "totalRecord" >
              </el-pagination>-->
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="角色分配：" :label-width="formLabelWidth" >
          <el-select v-model="roleType" clearable placeholder="请选择" @change="selectKey">
            <el-option v-for="item in role" :key="item.id" :label="item.role" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class>
        <el-button @click="useradd = false">取消</el-button>
        <el-button type="primary" @click="submitUserAdd">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑成员" :visible.sync="userEdit" :modal="false">
      <el-form :model="edit">
        <el-form-item label="姓名：" :label-width="formLabelWidth">
          <el-select
            v-model="userListEdit"
            filterable
            :disabled="true"
            :placeholder="tableData3.name"
            @change="userListESelectKey"
          >
            <el-option
              v-for="item in tableData3"
              :key="item.name"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        

        <el-form-item label="角色分配：" :label-width="formLabelWidth">
          <el-select v-model="roleTypeEdit" clearable :placeholder="role.role" @change="EselectKey">
            <el-option v-for="item in role" :key="item.id" :label="item.role" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class>
        <el-button @click="userEdit = false">取消</el-button>
        <el-button type="primary" @click="submitUserEdit">确定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
export default {
  data() {
    return {
      formLabelWidth: "150px",
      projectId: "",
      useradd: false,
      userEdit: false,
      tableData: [],
      tableData3: [],
      formoptions: [],
      formvalue: {},
      isIndeterminate: true,
      form: {},
      userList: [],
      userListEdit: [],
      role: [],
      multipleSelection: [],
      edit:{},
      roleType: [],
      roleTypeEdit: [],
      totalRecord: 0,
      totalPage: 0,
      currentPage: 1,
      listLoading: false,
      id: "",
      idEdit: ""
    };
  },
  methods: {
    selectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.role.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },
    EselectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.role.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },
    userListSelectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.tableData3.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },
    userListESelectKey(id) {
      this.id = id;
      let obj = {};
      obj = this.tableData3.find(item => {
        return item.id === id;
        this.id = obj.id;
      });
    },

    deleteRow(index, row) {
      this.$confirm("是否要删除?", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.projectModel
            .memberByRemove(row.id)
            .then(res => {
              ;
              this.tableData.splice(index, 1);
            })
            .catch(err => {});
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getMember()
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleEdit(index, row) {
      this.userEdit = true;
      this.idEdit = row.id;
      this.userListEdit = row.name;
      this.roleTypeEdit = row.role;
    },

    submitUserEdit() {
      let data = {
        id: this.idEdit,
        projectId: this.projectId,
        roleId: this.roleTypeEdit
      };
      console.log(data);
      this.projectModel
        .memberByUpdata(data)
        .then(res => {
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: res.msg
            });
          this.handleCurrentChange(event);
          }
          this.userEdit = false;
        })
        .catch(err => {});
    },
    handleCurrentChange(event) {
      this.projectModel
        .getAllMemberByPrjId(this.projectId, this.currentPage, "10")
        .then(res => {
          this.tableData = res.data.list;
        })
        .catch(err => {
          console.log(err.message);
        });
    },

    submitUserAdd() {
      let data = {
        projectId: this.projectId,
        userId: this.userList,
        roleId: this.roleType
      };
      console.log(data);
      this.projectModel
        .memberByAdd(data)
        .then(res => {
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: res.msg
            });
            this.getMember();
          }else if(res.status === 500){
            this.$message({
              type: "warning",
              message: res.msg
            });
          }
          this.useradd = false;
        })
        .catch(err => {
          console.log(err.message);
        });
    },
    handlePreClick(event) {
      if (this.currentPage != 1) {
        this.currentPage--;
      }
    },
    handleNextClick(event) {
      if (this.currentPage != this.totalPage) {
        this.currentPage++;
      }
    },
    getMember(){
      this.projectModel
      .getAllMemberByPrjId(this.projectId, "1", "10")
      .then(res => {
        this.totalRecord = res.data.totalRecord;
        this.totalPage = res.data.totalPage;
        this.tableData = res.data.list;
      })
      .catch(err => {
        console.log(err.message);
      });
    }
  },
  mounted() {
    this.projectId = this.$route.query.projectId;

    this.userModel
      .userList("1", "9999")
      .then(res => {
        this.tableData3 = res.data.list;
        ;
      })
      .catch(err => {
        console.log(err.message);
      });

    this.userModel
      .userRole()
      .then(res => {
        this.role = res.data;
      })
      .catch(err => {});

    this.projectModel
      .getAllMemberByPrjId(this.projectId, "1", "10")
      .then(res => {
        this.totalRecord = res.data.totalRecord;
        this.totalPage = res.data.totalPage;
        this.tableData = res.data.list;
      })
      .catch(err => {
        console.log(err.message);
      });
  }
};
</script>

<style >
.content {
  padding-top: 20px;
  background-color: #ffffff;
  width: 100%;
  height: 100%;
}
.button-add {
  margin-left: 20px;
}
.table {
  margin-top: 10px;
  margin-left: 10px;
  width: auto;
  margin-right: 10px;
}
.input-w-220 {
  width: 220px;
}
.transfer-footer {
  margin-left: 20px;
  padding: 6px 5px;
}
</style>